﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NMLX_Data_Management
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“nMLX_Data_MDataSet.Project”中。您可以根据需要移动或删除它。
            ProjectQuery("");
        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //点击了详情按钮
            if (e.ColumnIndex == 3)
            {
                int project = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                string proname = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[1].Value);
                new ProjectDetail(this, project, proname).Show();
                this.Hide();
                //MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void 新增程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddProgram addProgram = new AddProgram();
            addProgram.ShowDialog();

        }

        private void 新增项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            AddProject addProject = new AddProject();
            if (addProject.ShowDialog() == DialogResult.OK)
            {
                ProjectQuery("");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string where = " where ProName like '%" + toolStripTextBox1.Text + "%' or code like '%" + toolStripTextBox1.Text.ToUpper() + "%'";
            ProjectQuery(where);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="where"></param>
        public void ProjectQuery(string where)
        {

            string sql = string.Format(" select * from project {0} ", where);
            try
            {
                DataSet ds = new DataSet();
                ds = NMLX.DBUtility.DbHelperOleDb.Query(sql);
                dataGridView1.DataSource = ds.Tables[0];
                this.dataGridView1.Refresh();
                //this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Gray;
                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "错误");
            }


        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 清空数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearDb clearDb = new ClearDb();
            if (clearDb.ShowDialog() == DialogResult.OK)
            {
                ProjectQuery("");
            }
        }

        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddUsers addUsers = new AddUsers();
            if (addUsers.ShowDialog() == DialogResult.OK)
            {
                ProjectQuery("");
            }
        }

        private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r")
            {
                string where = " where ProName like '%" + toolStripTextBox1.Text + "%' or code like '%" + toolStripTextBox1.Text.ToUpper() + "%'";
                this.toolStripTextBox1.Text = "";
                ProjectQuery(where);

            }

        }

        private void 远程辅助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\NMLX_file\\燃料系统远程维护辅助表.xlsx");
            }

            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void 签出查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QCQuery qCQuery = new QCQuery();
            qCQuery.Show(this);
        }
    }
}
